package com.yang.lineChart;

import com.yang.common.CreatLineChart;
import com.yang.utils.Serie;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Vector;

public class CreateTPSLine {

    public static void createLine(String jtlFileName, String imgOutPath){
        try {

            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(jtlFileName), "utf-8"));//GBK
            String line = null;

            long minTime = 1696387337091L;
            long maxTime = 0L;
            int count = 0;
            String label = null;

            Vector<Object> TPS = new Vector<Object>();
            reader.readLine();
            while ((line = reader.readLine()) != null) {
                String item[] = line.split(",");
                String timeStamp = item[0];
                String elapsed = item[1];
                if (label == null){
                    label = item[2];
                }

                String responseCode = item[3];
                count++;

                if (Long.parseLong(timeStamp) < minTime) {
                    minTime = Long.parseLong(timeStamp);
                }

                if (maxTime < Long.parseLong(timeStamp)) {
                    maxTime = Long.parseLong(timeStamp);
                }

                long time = Long.parseLong(timeStamp) + Long.parseLong(elapsed);

                if ((time - minTime) >= 1000) {

                    float tps = (float) count * 1000 / (time - minTime);
                    minTime = Long.parseLong(timeStamp);
                    TPS.add(tps);
                    count = 0;
                }

//                ArrayList<String> arrayList = new ArrayList<>(item.length);
//                Collections.addAll(arrayList, item);
            }

            long step = (maxTime - minTime) / TPS.size();

            String timeStr;
            ArrayList<String> categorie = new ArrayList<String>();

            for (int i = 0; i < TPS.size(); i++) {
                int hours = (int) (((step * i+1) % (60 * 60 * 24)) / (60 * 60));
                int minutes = (int) (((step * i+1) % (60 * 60)) / 60);
                int seconds = (int) ((step * i+1) % 60);
                timeStr = unitFormat(hours) + ":" + unitFormat(minutes) + ":" + unitFormat(seconds);
                categorie.add(timeStr);
            }

            Serie serie = new Serie(label, TPS);
            ArrayList<Serie> series = new ArrayList<Serie>();
            series.add(serie);

            CreatLineChart.CreateNewLineChartForPng("TPS", "Elapsed time(granularity: 1 sec)", "Number of transactions/sec"
                    , imgOutPath, categorie, series, 900, 500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }
}
